/*
 * 原版接口
 */
import * as server from "@minecraft/server";
/*
 * 蛋白石组件
 */
import * as native_tools from "../system/native_tools";
import * as derived_tools from "../system/derived_tools";
/**
 * * < 方块 > 更新后 事件
 */
export function BlockUpdateAfterEvent(block: server.Block) {
    /**
     * * 执行 方块更新事件 的 方块列表
     */
    const blocks: server.Block[] = [];
    // 获取 进行更新的方块
    for (let axleX = -1; axleX <= 1; axleX++) for (let axleY = -1; axleY <= 1; axleY++) for (let axleZ = -1; axleZ <= 1; axleZ++) {
        /**
         * * 目标方块
         */
        const target = block.offset({ x: axleX, y: axleY, z: axleZ });
        if (target) blocks.push(target);
    };
    // 遍历 方块列表
    blocks.forEach(
        block => {
            switch (block.typeId) {
                case 'block_cable:enable_control': EnableControl(block); break;

                case 'block_cable:basic_pipeline': BasicPipeline(block); break;

                case 'block_cable:pulse_latch': PulseLatch(block); break;

                case 'block_gate:portal_above': PortalGate(block); break;

                case 'block_gate:portal_below': PortalGate(block); break;

                case 'block_gate:andesite': PlaneGate(block); break;

                case 'block_gate:birch': PlaneGate(block); break;

                case 'block_gate:basalt': PlaneGate(block); break;

                case 'block_gate:diorite': VerticalGate(block); break;

                case 'block_gate:deepslate': VerticalGate(block); break;

                case 'block_gate:spruce': VerticalGate(block); break;

                case 'block_decorating:jungle_wood_chair': JungleWoodChair(block); break;

                case 'block_decorating:diorite_table': DioriteTable(block); break;

                default: break;
            }
        }
    )
};

/**
 * * 魔导总线 - 基础总线
 */
function BasicPipeline(object: server.Block) {
    // 检测 方向x
    const direction_positive_x = object.east()?.hasTag('data_tag:magic_cable.port_negative.X') ?? false;
    const direction_negative_x = object.west()?.hasTag('data_tag:magic_cable.port_positive.X') ?? false;
    // 检测 方向y
    const direction_positive_y = object.above()?.hasTag('data_tag:magic_cable.port_negative.Y') ?? false;
    const direction_negative_y = object.below()?.hasTag('data_tag:magic_cable.port_positive.Y') ?? false;
    // 检测 方向z  
    const direction_positive_z = object.south()?.hasTag('data_tag:magic_cable.port_negative.Z') ?? false;
    const direction_negative_z = object.north()?.hasTag('data_tag:magic_cable.port_positive.Z') ?? false;
    // 赋值 方向X
    native_tools.TrySetPermutation(object, 'STATE:direction_positive.X', direction_positive_x);
    native_tools.TrySetPermutation(object, 'STATE:direction_negative.X', direction_negative_x);
    // 赋值 方向Y
    native_tools.TrySetPermutation(object, 'STATE:direction_positive.Y', direction_positive_y);
    native_tools.TrySetPermutation(object, 'STATE:direction_negative.Y', direction_negative_y);
    // 赋值 方向Z
    native_tools.TrySetPermutation(object, 'STATE:direction_positive.Z', direction_positive_z);
    native_tools.TrySetPermutation(object, 'STATE:direction_negative.Z', direction_negative_z);
};

/**
 * * 魔导总线 - 脉冲锁存
 */
function PulseLatch(object: server.Block) {
    // 检测 方向x
    const direction_positive_x = object.east()?.hasTag('data_tag:magic_cable.logic_negative.X') ? true : object.east()?.hasTag('data_tag:magic_cable.port_negative.X');
    const direction_negative_x = object.west()?.hasTag('data_tag:magic_cable.logic_positive.X') ? true : object.west()?.hasTag('data_tag:magic_cable.port_positive.X');
    // 检测 方向y
    const direction_positive_y = object.above()?.hasTag('data_tag:magic_cable.logic_negative.Y') ? true : object.above()?.hasTag('data_tag:magic_cable.port_negative.Y');
    const direction_negative_y = object.below()?.hasTag('data_tag:magic_cable.logic_positive.Y') ? true : object.below()?.hasTag('data_tag:magic_cable.port_positive.Y');
    // 检测 方向z  
    const direction_positive_z = object.south()?.hasTag('data_tag:magic_cable.logic_negative.Z') ? true : object.south()?.hasTag('data_tag:magic_cable.port_negative.Z');
    const direction_negative_z = object.north()?.hasTag('data_tag:magic_cable.logic_positive.Z') ? true : object.north()?.hasTag('data_tag:magic_cable.port_positive.Z');
    // 赋值 方向X
    native_tools.TrySetPermutation(object, 'STATE:direction_positive.X', direction_positive_x ?? false);
    native_tools.TrySetPermutation(object, 'STATE:direction_negative.X', direction_negative_x ?? false);
    // 赋值 方向Y
    native_tools.TrySetPermutation(object, 'STATE:direction_positive.Y', direction_positive_y ?? false);
    native_tools.TrySetPermutation(object, 'STATE:direction_negative.Y', direction_negative_y ?? false);
    // 赋值 方向Z
    native_tools.TrySetPermutation(object, 'STATE:direction_positive.Z', direction_positive_z ?? false);
    native_tools.TrySetPermutation(object, 'STATE:direction_negative.Z', direction_negative_z ?? false);
};

/**
 * * 魔导总线 - 传播许可
 */
function EnableControl(object: server.Block) {
    /**
     * * 判断输出
     */
    const output = [
        object.east(),
        object.west(),
        object.south(),
        object.north()
    ].filter(block => block?.hasTag('data_tag:magic_cable.series')).length;
    /**
     * * 判断控制
     */
    const control = [
        object.above(),
        object.below()
    ].filter(block => block?.hasTag('data_tag:magic_cable.latch')).length;
    // 赋值 方块状态
    server.system.runTimeout(() => native_tools.TrySetPermutation(object, 'STATE:output', output != 0 ? 1 : 0), 1);
    server.system.runTimeout(() => native_tools.TrySetPermutation(object, 'STATE:control', control != 0 ? 1 : 0), 2);
};

/**
 * * 机关之门 - 魔晶传送
 */
function PortalGate(object: server.Block) {
    // 查询方块标签
    const direction_0 = object.west()?.hasTag('data_tag:magic_portal.series') ?? false;
    const direction_1 = object.south()?.hasTag('data_tag:magic_portal.series') ?? false;
    const direction_2 = object.east()?.hasTag('data_tag:magic_portal.series') ?? false;
    const direction_3 = object.north()?.hasTag('data_tag:magic_portal.series') ?? false;
    // 赋值 方块状态
    server.system.runTimeout(() => native_tools.TrySetPermutation(object, 'STATE:direction_0', direction_0 ? 1 : 0), 1);
    server.system.runTimeout(() => native_tools.TrySetPermutation(object, 'STATE:direction_1', direction_1 ? 1 : 0), 2);
    server.system.runTimeout(() => native_tools.TrySetPermutation(object, 'STATE:direction_2', direction_2 ? 1 : 0), 3);
    server.system.runTimeout(() => native_tools.TrySetPermutation(object, 'STATE:direction_3', direction_3 ? 1 : 0), 4);
};

/**
 * * 水平放置 的 机关之门
 * 
 * @param {server.Block} object - 机关门对象
 */
function PlaneGate(object: server.Block) {
    // 查询方块标签
    const direction_0 = object.east()?.typeId == object.typeId;
    const direction_1 = object.west()?.typeId == object.typeId;
    const direction_2 = object.south()?.typeId == object.typeId;
    const direction_3 = object.north()?.typeId == object.typeId;
    // 赋值 方块状态
    server.system.runTimeout(() => native_tools.TrySetPermutation(object, 'STATE:direction_0', direction_0 ? 1 : 0), 1);
    server.system.runTimeout(() => native_tools.TrySetPermutation(object, 'STATE:direction_1', direction_1 ? 1 : 0), 2);
    server.system.runTimeout(() => native_tools.TrySetPermutation(object, 'STATE:direction_2', direction_2 ? 1 : 0), 3);
    server.system.runTimeout(() => native_tools.TrySetPermutation(object, 'STATE:direction_3', direction_3 ? 1 : 0), 4);
};

/**
 * * 垂直放置 的 机关之门
 * 
 * @param {server.Block} object - 机关门对象
 */
function VerticalGate(object: server.Block) {
    // 方块状态默认值
    let about = 0, center = 0;
    // 修饰 机关门 朝向
    if (object.west()?.typeId == object.typeId) about = 1;
    else if (object.south()?.typeId == object.typeId) about = 2;
    else if (object.east()?.typeId == object.typeId) about = 3;
    else if (object.north()?.typeId == object.typeId) about = 4;
    // 修饰 机关门 边框
    if (object.east()?.typeId == object.typeId && object.west()?.typeId == object.typeId) center = 1;
    else if (object.south()?.typeId == object.typeId && object.north()?.typeId == object.typeId) center = 1;
    // 校验 机关门朝向
    if (about == 0) return;
    server.system.runTimeout(() => native_tools.TrySetPermutation(object, 'STATE:about', about), 1);
    server.system.runTimeout(() => native_tools.TrySetPermutation(object, 'STATE:center', center), 2);
};

/**
 * * 丛林木椅
 * 
 * @param {server.Block} object - 机关门对象
 */
function JungleWoodChair(object: server.Block) {
    if (object.permutation.getState('STATE:stage') != 0) {
        switch (object.permutation.getState('minecraft:cardinal_direction')) {
            case 'south':
                if (object.east()?.typeId != object.typeId && object.west()?.typeId != object.typeId)
                    native_tools.TrySetPermutation(object, 'STATE:stage', 0);
                break;

            case 'north':
                if (object.east()?.typeId != object.typeId && object.west()?.typeId != object.typeId)
                    native_tools.TrySetPermutation(object, 'STATE:stage', 0);
                break;

            case 'east':
                if (object.south()?.typeId != object.typeId && object.north()?.typeId != object.typeId)
                    native_tools.TrySetPermutation(object, 'STATE:stage', 0);
                break;

            case 'west':
                if (object.south()?.typeId != object.typeId && object.north()?.typeId != object.typeId)
                    native_tools.TrySetPermutation(object, 'STATE:stage', 0);
                break;

            default: break;
        }
    }
    else if (object.permutation.getState('STATE:stage') == 0 && object.permutation.getState('STATE:type') == 0) {
        switch (object.permutation.getState('minecraft:cardinal_direction')) {
            case 'south':
                if (object.east()?.hasTag('data_tag:home_decorating.chair.south')) native_tools.TrySetPermutation(object, 'STATE:stage', 1);
                else if (object.west()?.hasTag('data_tag:home_decorating.chair.south')) native_tools.TrySetPermutation(object, 'STATE:stage', 2);
                break;

            case 'north':
                if (object.east()?.hasTag('data_tag:home_decorating.chair.north')) native_tools.TrySetPermutation(object, 'STATE:stage', 2);
                else if (object.west()?.hasTag('data_tag:home_decorating.chair.north')) native_tools.TrySetPermutation(object, 'STATE:stage', 1);
                break;

            case 'east':
                if (object.south()?.hasTag('data_tag:home_decorating.chair.east')) native_tools.TrySetPermutation(object, 'STATE:stage', 2);
                else if (object.north()?.hasTag('data_tag:home_decorating.chair.east')) native_tools.TrySetPermutation(object, 'STATE:stage', 1);
                break;

            case 'west':
                if (object.south()?.hasTag('data_tag:home_decorating.chair.west')) native_tools.TrySetPermutation(object, 'STATE:stage', 1);
                else if (object.north()?.hasTag('data_tag:home_decorating.chair.west')) native_tools.TrySetPermutation(object, 'STATE:stage', 2);
                break;

            default: break;
        }
    }
};

/**
 * * 闪长岩桌
 * 
 * @param {server.Block} object - 机关门对象
 */
function DioriteTable(object: server.Block) {
    if (object.permutation.getState('STATE:stage') != 0) {
        switch (object.permutation.getState('minecraft:cardinal_direction')) {
            case 'south':
                if (object.east()?.hasTag('data_tag:home_decorating.table.south')) {
                    if (object.west()?.hasTag('data_tag:home_decorating.table.south'))
                        native_tools.TrySetPermutation(object, 'STATE:stage', 3);
                }
                else if (!object.east()?.hasTag('data_tag:home_decorating.table.south')) {
                    if (!object.west()?.hasTag('data_tag:home_decorating.table.south'))
                        native_tools.TrySetPermutation(object, 'STATE:stage', 0);
                };
                break;

            case 'north':
                if (object.east()?.hasTag('data_tag:home_decorating.table.north')) {
                    if (object.west()?.hasTag('data_tag:home_decorating.table.north'))
                        native_tools.TrySetPermutation(object, 'STATE:stage', 3);
                }
                else if (!object.east()?.hasTag('data_tag:home_decorating.table.north')) {
                    if (!object.west()?.hasTag('data_tag:home_decorating.table.north'))
                        native_tools.TrySetPermutation(object, 'STATE:stage', 0);
                };
                break;

            case 'east':
                if (object.south()?.hasTag('data_tag:home_decorating.table.east')) {
                    if (object.north()?.hasTag('data_tag:home_decorating.table.east'))
                        native_tools.TrySetPermutation(object, 'STATE:stage', 3);
                }
                else if (!object.south()?.hasTag('data_tag:home_decorating.table.east')) {
                    if (!object.north()?.hasTag('data_tag:home_decorating.table.east'))
                        native_tools.TrySetPermutation(object, 'STATE:stage', 0);
                };
                break;

            case 'west':
                if (object.south()?.hasTag('data_tag:home_decorating.table.west')) {
                    if (object.north()?.hasTag('data_tag:home_decorating.table.west'))
                        native_tools.TrySetPermutation(object, 'STATE:stage', 3);
                }
                else if (!object.south()?.hasTag('data_tag:home_decorating.table.west')) {
                    if (!object.north()?.hasTag('data_tag:home_decorating.table.west'))
                        native_tools.TrySetPermutation(object, 'STATE:stage', 0);
                };
                break;

            default: break;
        }
    }
    else if (object.permutation.getState('STATE:stage') == 0) {
        switch (object.permutation.getState('minecraft:cardinal_direction')) {
            case 'south':
                if (object.east()?.hasTag('data_tag:home_decorating.table.south') && object.west()?.hasTag('data_tag:home_decorating.table.south')) native_tools.TrySetPermutation(object, 'STATE:stage', 3);
                else if (object.east()?.hasTag('data_tag:home_decorating.table.south')) native_tools.TrySetPermutation(object, 'STATE:stage', 2);
                else if (object.west()?.hasTag('data_tag:home_decorating.table.south')) native_tools.TrySetPermutation(object, 'STATE:stage', 1);
                break;

            case 'north':
                if (object.east()?.hasTag('data_tag:home_decorating.table.north') && object.west()?.hasTag('data_tag:home_decorating.table.north')) native_tools.TrySetPermutation(object, 'STATE:stage', 3);
                else if (object.east()?.hasTag('data_tag:home_decorating.table.north')) native_tools.TrySetPermutation(object, 'STATE:stage', 1);
                else if (object.west()?.hasTag('data_tag:home_decorating.table.north')) native_tools.TrySetPermutation(object, 'STATE:stage', 2);
                break;

            case 'east':
                if (object.south()?.hasTag('data_tag:home_decorating.table.east') && object.north()?.hasTag('data_tag:home_decorating.table.east')) native_tools.TrySetPermutation(object, 'STATE:stage', 3);
                else if (object.south()?.hasTag('data_tag:home_decorating.table.east')) native_tools.TrySetPermutation(object, 'STATE:stage', 1);
                else if (object.north()?.hasTag('data_tag:home_decorating.table.east')) native_tools.TrySetPermutation(object, 'STATE:stage', 2);
                break;

            case 'west':
                if (object.south()?.hasTag('data_tag:home_decorating.table.west') && object.north()?.hasTag('data_tag:home_decorating.table.west')) native_tools.TrySetPermutation(object, 'STATE:stage', 3);
                else if (object.south()?.hasTag('data_tag:home_decorating.table.west')) native_tools.TrySetPermutation(object, 'STATE:stage', 2);
                else if (object.north()?.hasTag('data_tag:home_decorating.table.west')) native_tools.TrySetPermutation(object, 'STATE:stage', 1);
                break;

            default: break;
        };
        // 有 15% 概率显示隐藏合成配方
        if (derived_tools.IsEnable(15)) server.system.run(() => native_tools.TrySetPermutation(object, 'STATE:conceal', 1));
    }
};